package hyl.core.struct;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import hyl.core.run.IDo1;
import hyl.core.run.IFilter;

/** 未测试版本,后期代替 TjTreeNode */
public class MyNode<T> {
	public T _id = null;
	public T _id父节点 = null;
	public MyNode<T> _nd父节点 = null;
	/**
	 * 附件
	 */
	public String value;
	public Map<String, Object> _tag = new HashMap<>();
	int _i位置 = 0;
	public List<MyNode<T>> _nd子节点 = new ArrayList<>();

	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < _nd子节点.size(); i++)
			sb.append(_nd子节点.get(i)._id).append(",");
		if (sb.length() > 0)
			sb.setLength(sb.length() - 1);
		String zjd = sb.toString();
		sb.setLength(0);
		sb.append("{id:").append(_id).append(",").append("i父节点:").append(_id父节点).append(",").append("i位置:").append(_i位置)
				.append(",").append("子节点:[").append(zjd).append("]").append(",tag:").append(JSON.toJSONString(_tag))
				.append("}");
		return sb.toString();
	}

	public MyNode(T 编号) {
		this._id = 编号;
	}

	public int getI位置() {
		return _i位置;
	}

	// 不要制作层数属性 ,通过递归遍历获取
	public MyNode<T> get子节点(int 区位) {
		if (_nd子节点.size() > 区位) {
			return _nd子节点.get(区位);
		}
		return null;
	}

	public void set父节点(MyNode<T> 新父节点) {
		MyNode<T> 原父节点 = this._nd父节点;
		if (原父节点 != null) {
			原父节点._nd子节点.remove(this);
		}
		新父节点._nd子节点.add(this);
		this._id父节点 = 新父节点._id;
		this._nd父节点 = 新父节点;
	}

	public boolean set父节点(MyNode<T> 新父节点, int 区位) {
		if (新父节点.get子节点(区位) != null)
			return false;
		if (this._nd父节点 != null)
			this._nd父节点.remove(this);
		新父节点._nd子节点.add(this);
		this._id父节点 = 新父节点._id;
		this._nd父节点 = 新父节点;
		return true;
	}

	public void remove(MyNode<T> dy) {
		dy._nd父节点 = null;
		_nd子节点.remove(dy);
	}

	public int get子节点数() {
		return _nd子节点.size();
	}

	public T get_id() {
		return _id;
	}

	public void set_id(T _id) {
		this._id = _id;
	}

	public T get_id父节点() {
		return _id父节点;
	}

	public void set_id父节点(T _id父节点) {
		this._id父节点 = _id父节点;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * 向上遍历所有,处理某事
	 * 
	 * @throws Exception
	 */
	public void up(IDo1<MyNode<T>> worker) {
		if (worker == null) {
			return;
		}
		worker.run(this);
		if (_nd父节点 == null)
			return;
		this._nd父节点.up(worker);
	}

	/**
	 * 从自身开始向上遍历,直到满足条件停止遍历
	 * 
	 */
	public boolean upUntil(IFilter<MyNode<T>> worker) throws Exception {
		if (worker == null) {
			return false;
		}
		if (worker.where(this))
			return true;
		if (_nd父节点 == null)
			return false;
		if (this._nd父节点.upUntil(worker))
			return true;
		return false;
	}

	/**
	 * 从自身节点向下遍历节点
	 */
	public void down(IDo1<MyNode<T>> beforeworker, IDo1<MyNode<T>> afterworker) {
		if (beforeworker != null)
			beforeworker.run(this);
		for (MyNode<T> user : this._nd子节点) {
			if (user == null)
				continue;
			else {
				user.down(beforeworker, afterworker);
			}
		}
		if (afterworker != null)
			afterworker.run(this);
	}

	/**
	 * 向下遍历,直到满足条件停止遍历
	 * 
	 * @param beforeworker
	 * @param afterworker
	 * @return
	 * @throws Exception
	 */
	public boolean downUntil(IFilter<MyNode<T>> beforeworker, IFilter<MyNode<T>> afterworker) throws Exception {
		if (beforeworker != null)
			if (beforeworker.where(this))
				return true;
		for (MyNode<T> user : this._nd子节点) {
			if (user == null)
				continue;
			else {
				if (user.downUntil(beforeworker, afterworker))
					return true;
			}
		}
		if (afterworker != null)
			if (afterworker.where(this))
				return true;
		return false;
	}

	public JSONObject toJson() {
		JSONObject jso = new JSONObject();
		jso.put("id", _id);
		// jso.put("pid", _id父节点);
		// jso.put("index", _i位置);
		for (Entry<String, Object> e : _tag.entrySet()) {
			jso.put(e.getKey(), e.getValue());
		}
		JSONArray jsa = new JSONArray();
		for (MyNode<T> mn : _nd子节点) {
			jsa.add(mn.toJson());
		}
		jso.put("children", jsa);
		return jso;
	}
}
